﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH.Models;
using QLBH.Utilities;

namespace QLBH.Modules
{
    public partial class frmAdd_Edit_Staff : Form
    {
        private eType type = eType.ADD;
        private StaffModel Objmodel;
        public StaffModel ObjModel
        {
            get { return Objmodel; }
            set { Objmodel = value; }
        }

        public frmAdd_Edit_Staff(eType type)
        {
            InitializeComponent();
            Objmodel = new StaffModel();
            this.type = type;
        }


        private void frmAdd_Edit_Staff_Load(object sender, EventArgs e)
        {
            if (type == eType.EDIT)
            {
                txbHoten.Text = ObjModel.Name;
                txbTaikhoan.Text = ObjModel.Username;
                txbDiachi.Text = ObjModel.Address;
                dtpNgaysinh.Text = ObjModel.Birthdate;
                txbDienthoai.Text = ObjModel.Phone.ToString();
                txbEmail.Text = ObjModel.Email;
                txbCMND.Text = ObjModel.People_id.ToString();
                txbMucluong.Text = ObjModel.Salary.ToString();
                txbGhichu.Text = ObjModel.Comment;
                txbTaikhoan.ReadOnly = true;
                txbMatkhau.ReadOnly = true;
                txbMatkhauXn.ReadOnly = true;
            }
        }
        public bool checkData()
        {
            try
            {
                DataInputHelper.CheckTextData(txbTaikhoan.Text, "Tài khoản", 50);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông tin tài khoản", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txbTaikhoan.SelectAll();
                txbTaikhoan.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextData(txbMatkhau.Text, "Mật khẩu", 30);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông tin tài khoản", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txbMatkhau.SelectAll();
                txbMatkhau.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckConfigPassWord(txbMatkhau.Text, txbMatkhauXn.Text, "Mật khẩu xác nhận");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông tin tài khoản", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txbMatkhauXn.SelectAll();
                txbMatkhauXn.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextData(txbHoten.Text, "Họ tên", 50);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông tin tài khoản", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txbHoten.SelectAll();
                txbHoten.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextData(dtpNgaysinh.Text, "Ngày sinh", 50);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông tin tài khoản", MessageBoxButtons.OK, MessageBoxIcon.Error);
                dtpNgaysinh.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextDataLength(txbDiachi.Text, "Địa chỉ", 100);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông tin tài khoản", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txbDiachi.SelectAll();
                txbDiachi.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckPhoneNumber(txbDienthoai.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông tin tài khoản", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txbDienthoai.SelectAll();
                txbDienthoai.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckEmailData(txbEmail.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông tin tài khoản", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txbEmail.SelectAll();
                txbEmail.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckIntValue(txbCMND.Text, "Số CMND");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông tin tài khoản", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txbCMND.SelectAll();
                txbCMND.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckDecimalData(txbMucluong.Text, "Mức lương");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông tin tài khoản", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txbMucluong.SelectAll();
                txbMucluong.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextDataLength(txbGhichu.Text, "Ghi chú", 255);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông tin tài khoản", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txbGhichu.SelectAll();
                txbGhichu.Focus();
                return false;
            }
            return true;
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (checkData())
            {
                int shop_id = int.Parse(MainForm.Instance.Shop_id.ToString());
                ObjModel.Username = txbTaikhoan.Text;
                ObjModel.Password = txbMatkhauXn.Text;
                ObjModel.Name = txbHoten.Text;
                DateTime date = DateTime.Parse(dtpNgaysinh.Text);
                ObjModel.Birthdate = date.ToString("yyyy-MM-dd");
                ObjModel.Address = txbDiachi.Text;
                ObjModel.People_id = int.Parse(txbCMND.Text);
                ObjModel.Phone = int.Parse(txbDienthoai.Text);
                ObjModel.Salary = int.Parse(txbMucluong.Text);
                ObjModel.Email = txbEmail.Text;
                ObjModel.Comment = txbGhichu.Text;
                ObjModel.Shop_id = shop_id;
                if (type == eType.ADD)
                {
                    try
                    {

                        RespontStaff response = WSAccess.Insert<StaffModel, RespontStaff>(ObjModel, "1/staff/insert");
                        if (response.Status != null)
                        {
                            MessageBox.Show("Đăng ký thành công");
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    try
                    {
                        WSAccess.Update<StaffModel>(ObjModel, ObjModel.Id + "/staff/edit");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                this.DialogResult = DialogResult.None;
            }

        }
        private void btnHuy_Click(object sender, EventArgs e)
        {

        }
    }
    public class RespontStaff : ResponseBase
    {
        private int staffid;

        public int Staffid
        {
            get { return staffid; }
            set { staffid = value; }
        }

    }
}
